package com.hyyt.shopjsp.wxmg.basicInfo.controller;

import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import com.hyyt.shopjsp.wxmg.basicInfo.pojo.WxGlobalReturnCode;
import com.hyyt.shopjsp.wxmg.basicInfo.service.IWxGlobalReturnCodeService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator on 2017/8/6.
 */
@Controller
@RequestMapping("/back/wx/globalReturnCode")
public class WxGlobalReturnCodeController extends BaseController{

    private Logger logger = Logger.getLogger(WxGlobalReturnCodeController.class);
    private String basePath = "/back/wxmg/globalReturnCode/";

    @Resource
    private IWxGlobalReturnCodeService wxGlobalReturnCodeService;

    /**
     * 进入全局返回码页面
     *
     * @return 返回到指定页面
     */
    @RequestMapping("gotoGlobalReturnCodePage")
    public String gotoGlobalReturnCodePage (){
        return basePath + "globalReturnCodeList";
    }

    /**
     *查询所有全局返回码列表
     * @param page
     * @param response
     */
    @RequestMapping(value = "listGlobalReturnCode",method = RequestMethod.POST)
    @ResponseBody
    public void listGlobalReturnCode(String returncode, Page page, HttpServletResponse response){
        Map<String,Object> param = new HashMap<>();
        if (Utils.objectIsNotEmpty(returncode)) {
            param.put("returncode", returncode.trim());
        }
        param.put("sortColumns","globalreturncodeId desc");
        int totalRecordCount = wxGlobalReturnCodeService.countWxGlobalReturnCode(param);
        page.setPageInfo(page.getPageSize(),totalRecordCount,page.getCurrentPage());
        List<WxGlobalReturnCode> list = wxGlobalReturnCodeService.queryWxGlobalReturnCode(param,page.getPageRecordBeginIndex(),page.getPageSize());
        param.clear();
        param.put("total",totalRecordCount);
        param.put("rows",list);
        ResultMsg.create(param,response);
    }

    /**
     * 根据id获得全局返回码对象
     * @param globalreturncodeId
     * @return
     */
    @RequestMapping(value = "getGlobalReturnCodeObject",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg getGlobalReturnCodeObject(Integer globalreturncodeId){
        if (Utils.objectIsNotEmpty(globalreturncodeId)){
            WxGlobalReturnCode wxGlobalReturnCode = wxGlobalReturnCodeService.loadWxGlobalReturnCode(globalreturncodeId);
            return ResultMsg.create(true,null, FastJsonUtils.toJSONString(wxGlobalReturnCode));
        }
        return ResultMsg.create(false);
    }

    /**
     * 根据id删除全局返回码
     * @param ids
     * @return
     */
    @RequestMapping(value = "deleteGlobalReturnCode",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg deleteGlobalReturnCode(Integer ids){
        if (Utils.objectIsNotEmpty(ids)){
            wxGlobalReturnCodeService.deleteWxGlobalReturnCode(ids);
        }
        return ResultMsg.create(true);
    }

    /**
     * 保存或者修改全局返回码
     * @param wxGlobalReturnCode
     * @return
     */
    @RequestMapping(value = "saveOrUpdateGlobalReturnCode",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg saveOrUpdateGlobalReturnCode(WxGlobalReturnCode wxGlobalReturnCode){
        if (Utils.objectIsNotEmpty(wxGlobalReturnCode)){
            if (Utils.objectIsEmpty(wxGlobalReturnCode.getGlobalreturncodeId())){
                wxGlobalReturnCodeService.saveWxGlobalReturnCode(wxGlobalReturnCode);
            }else{
                wxGlobalReturnCodeService.updateWxGlobalReturnCode(wxGlobalReturnCode);
            }
        }
        return ResultMsg.create(true);
    }

}
